﻿function getBits(number) {
  var digits = number.toString(2).split('');
  var result = new Array();
  // padding of zeros
  for (var i = 0; i < (4 - digits.length); i++) {
    result.push(0);
  }
  for (var j = (4 - digits.length); j < 4; j++) {
    result.push(parseInt(digits[j - i]));
  }
  return result;
}

function dateToString(date) {
  var result = addPadding(date.getHours()) + addPadding(date.getMinutes()) + addPadding(date.getSeconds());
  return result; // hhmmss
}

function addPadding(integer) {
  var string = integer > 9 ? integer : '0' + integer;
  return string + '';
}

function stringToArrayOfIntegers(string) {
  var strings = string.split('');
  var result = new Array();
  for (var i = 0; i < strings.length; i++) {
    result.push(parseInt(strings[i]));
  }
  return result;
}

function BinaryClock() {
  this.getBinaries = function (date) {
    var string = dateToString(date);
    var digits = stringToArrayOfIntegers(string);
    var binaries = new Array();
    for (var i = 0; i < digits.length; i++) {
      binaries.push(getBits(digits[i]));
    }
    return binaries;
  };
}
